﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid.Drawing;
using DevExpress.XtraGrid.Columns;

namespace AC
{
	public class MyCellMergeHelper
	{
		private List<MyMergedCell> _MergedCells = new List<MyMergedCell>();
		public List<MyMergedCell> MergedCells
		{
			get { return _MergedCells; }
		}

		MyGridPainter painter;
		GridView _view;

		public MyCellMergeHelper(GridView view)
		{
			_view = view;
			view.CustomDrawCell += new RowCellCustomDrawEventHandler(view_CustomDrawCell);
			view.GridControl.Paint += new PaintEventHandler(GridControl_Paint);
			view.CellValueChanged += new CellValueChangedEventHandler(view_CellValueChanged);
			painter = new MyGridPainter(view);
		}

		public MyMergedCell AddMergedCell(int rowHandle, int col1Index, int col2Index)
		{
			MyMergedCell cell = new MyMergedCell(rowHandle, col1Index, col2Index);
			_MergedCells.Add(cell);
			return cell;
		}

		public void AddMergedCell(int rowHandle, int col1Index, int col2Index, object value)
		{
			MyMergedCell cell = AddMergedCell(rowHandle, col1Index, col2Index);
			SafeSetMergedCellValue(cell, value);
		}

		public void AddMergedCell(int rowHandle, object value, int fromIndex)
		{
			int length = _view.VisibleColumns.Count;
			for (int i = fromIndex; i < length - 1; i++)
			{
				MyMergedCell cell = AddMergedCell(rowHandle, _view.VisibleColumns[i].AbsoluteIndex, _view.VisibleColumns[i + 1].AbsoluteIndex);
				SafeSetMergedCellValue(cell, value);
			}
		}

		public void SafeSetMergedCellValue(MyMergedCell cell, object value)
		{
			if (cell != null)
			{
				SafeSetCellValue(cell.RowHandle, _view.Columns[cell.Column1], value);
				SafeSetCellValue(cell.RowHandle, _view.Columns[cell.Column2], value);
			}
		}

		public void SafeSetCellValue(int rowHandle, GridColumn column, object value)
		{
			if (_view.GetRowCellValue(rowHandle, column) != value)
				_view.SetRowCellValue(rowHandle, column, value);
		}

		private MyMergedCell GetMergedCell(int rowHandle, GridColumn column)
		{
			foreach (MyMergedCell cell in _MergedCells)
			{
				if (cell.RowHandle == rowHandle && (column.AbsoluteIndex == cell.Column1 || column.AbsoluteIndex == cell.Column2))
					return cell;
			}
			return null;
		}

		private bool IsMergedCell(int rowHandle, GridColumn column)
		{
			return GetMergedCell(rowHandle, column) != null;
		}

		private void DrawMergedCells(PaintEventArgs e)
		{
			foreach (MyMergedCell cell in _MergedCells)
			{
				painter.DrawMergedCell(cell, e);
			}
		}

		void view_CellValueChanged(object sender, CellValueChangedEventArgs e)
		{
			SafeSetMergedCellValue(GetMergedCell(e.RowHandle, e.Column), e.Value);
		}

		void GridControl_Paint(object sender, PaintEventArgs e)
		{
			DrawMergedCells(e);
		}

		void view_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
		{
			if (IsMergedCell(e.RowHandle, e.Column))
				e.Handled = !painter.IsCustomPainting;
		}
	}
}
